﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using System.Data.SqlClient;
using jiaju.Areas.Back.Models.DBModel;
using jiaju.Areas.Back.Models.DAL;
using jiaju.Areas.Back.Models;
namespace jiaju.Areas.Back.Controllers
{
    [Authorize]
    public class HomeController : Controller
    {
        // GET: Back/Home
        public ActionResult Index()
        {
           
            User user = UserService.GetAlluser();
            ViewBag.A = user;
            return View();
        }
        [AllowAnonymous]
        public ActionResult Login()
        {
            string UserName = HttpContext.User.Identity.Name;
            if (UserName != null && UserName != "")
            {
                return RedirectToAction("Index");
            }
            else
            {
                return View("Login");

            }
        }
        [AllowAnonymous]
        [HttpPost]
        public ActionResult DelLogin(string UserName, string Password)
        {
            User use = UserService.getUser(UserName);
            if (use == null)
            {
                Result result = new Result()
                {
                    success = false,
                    message = "用户名不存在"
                };
                return Json(result);
            }
            else
            {
                if (Password == use.PassWord)
                {
                    FormsAuthentication.SetAuthCookie(use.UserName, true);
                    Result result = new Result()
                    {
                        success = true,
                        message = "登录成功"
                    };
                    return Json(result);
                }
                else
                {
                    Result result = new Result()
                    {
                        success = false,
                        message = "密码错误"
                    };
                    return Json(result);
                }
            }
        }

        public ActionResult Welcome()
        {
            string sql1 = "select COUNT(*) from dbo.product";
            int num1 = (int)DBHelp.ExcuteScalar(sql1);
            string sql2 = "select COUNT(*) from dbo.news";
            int num2 = (int)DBHelp.ExcuteScalar(sql2);
            string sql3 = "select COUNT(*) from dbo.Message";
            int num3 = (int)DBHelp.ExcuteScalar(sql3);
            ViewBag.A = num1;
            ViewBag.B = num2;
            ViewBag.C = num3;
            return View();

        }
        //个人信息
        public ActionResult UserInfo()
        {
            //个人信息
            string UserName = HttpContext.User.Identity.Name;//获取用户名
            User user = UserService.getUser(UserName);
            ViewBag.A = user;
            return View();

        }
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult EditDel(User user)
        {
            //编辑，请求数据库
            int n = UserService.EditArticle(user);
            if (n > 0)
            {
                return Json(new { success = true, msg = "编辑成功" });
            }
            else
            {
                return Json(new { success = false, msg = "编辑失败" });
            }
        }

        //修改密码
        public ActionResult ResetPwd()
        {
            User art = UserService.GetById();
            //把详情数据传到页面
            ViewBag.A = art;
            return View();

        }
        public ActionResult EditPWD(string UserName, string Password, string NewPassword)
        {
            User user = UserService.getUser(UserName);
            if (!Password.Equals(user.PassWord))
            {
                return Json(new { success = false, msg = "旧密码错误" });
            }
            else
            {
                //编辑，请求数据库
                int n = UserService.Editpwd(UserName, NewPassword);
                if (n > 0)
                {
                    FormsAuthentication.SignOut();
                    return Json(new { success = true, msg = "修改成功" });
                }
                else
                {
                    return Json(new { success = false, msg = "修改失败" });
                }
            }

        }
        //Exit退出
        public ActionResult Exit()
        {
            FormsAuthentication.SignOut(); //清除通行证
                                           //跳转登录页面
            return RedirectToAction("Login");
        }
        //处理图片上传
        [HttpPost]//post请求
        public ActionResult UploadPhoto()
        {
            //1，获取上传图片的对象uploadImg标签的name值
            HttpPostedFileBase postFile = Request.Files["uploadImg"];
            if (postFile != null)
            {
                //2.图片传过来，改名字， 用当前时间，
                string newFileName = DateTime.Now.ToString("yyyyMMddHHmmssffff") + ".png";
                //3.图片存到哪
                string uploadPath = "~/images/";
                //4.获取存储文件的物理路径（获取全路径）
                string fullUploadPath = Server.MapPath(uploadPath);
                //5.存上这张图片（保存图片）
                postFile.SaveAs(fullUploadPath + newFileName);//（路径+名称）
                                                              //返回地址
                return Json(new { success = true, path = "images/" + newFileName, msg = "上传成功" });
            }
            else
            {
                return Json(new { success = false, path = "", msg = "上传失败" });
            }

        }

    }
}